# 全局变量
filename = "tasklist.txt"
task_list = []

def load_tasks():
    global task_list
    try:
        with open(filename, "r") as taskfile:
            for line in taskfile:
                if line.strip():
                    task_list.append(line.strip().split(' | '))
    except FileNotFoundError:
        pass

def add_task(priority, description, date, task):
    global task_list
    with open(filename, 'a') as taskfile:
        taskfile.write(f"{priority} | {description} | {date} | {task}\n")
    task_list.append((priority, description, date, task))

def delete_task(index):
    global task_list
    if 0 <= index < len(task_list):
        del task_list[index]
        with open(filename, 'w') as taskfile:
            for t in task_list:
                taskfile.write(f"{t[0]} | {t[1]} | {t[2]} | {t[3]}\n")

def edit_task(index, priority, description, date, task):
    global task_list
    if 0 <= index < len(task_list):
        task_list[index] = (priority, description, date, task)
        with open(filename, 'w') as taskfile:
            for t in task_list:
                taskfile.write(f"{t[0]} | {t[1]} | {t[2]} | {t[3]}\n")

def get_tasks():
    global task_list
    return [f"{t[0]} | {t[1]} | {t[2]} | {t[3]}" for t in task_list]

# 初始化时加载任务
load_tasks()